package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.SetmealDTO;
import com.itheima.exception.CustomException;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealDish;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    @Transactional
    public void addSetmeal(SetmealDTO setmealDto) {
        //添加套餐表
        super.save(setmealDto);
        //添加套餐与菜品的关系表
        //关系表中没有套餐id，所以要循环添加进去
        for (SetmealDish setmealDish : setmealDto.getSetmealDishes()) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        setmealDishService.saveBatch(setmealDto.getSetmealDishes());

    }

    @Override
    @Transactional
    public void deleteWithDish(List<Long> ids) {
        //1.查询套餐状态是否可以删除
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus,1);
        setmealLambdaQueryWrapper.in(Setmeal::getId,ids);
        int count = super.count(setmealLambdaQueryWrapper);
        if (count > 0) {
            //1(1)如果状态为1的套餐，则表示该套餐正在售卖，不能删除，则抛出一个业务异常
            throw new CustomException("该套餐正在售卖中，不能删除");
        }
        //1(2)如果状态为禁用，则先删除套餐表中的数据
        super.removeByIds(ids);
        //2.删除关系表中的数据
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(setmealDishLambdaQueryWrapper);

    }

    @Override
    public SetmealDTO querySetmealInfoById(Long id) {
        Setmeal setmeal = this.getById(id);
        SetmealDTO setmealDTO = new SetmealDTO();
        BeanUtils.copyProperties(setmeal,setmealDTO);

        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> list = setmealDishService.list(setmealDishLambdaQueryWrapper);
        setmealDTO.setSetmealDishes(list);
        return setmealDTO;
    }

    @Override
    @Transactional
    public void updateSetmealInfoById(SetmealDTO setmealDTO) {
        //1.根据ID修改套餐表内容
        super.updateById(setmealDTO);
        //2.根据套餐id把套餐菜品关联表里的数据进行物理删除
        setmealDishService.deleteById(setmealDTO.getId());
        //3.重新循环添加套餐菜品关联数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDTO.getId());
        }
        setmealDishService.saveBatch(setmealDTO.getSetmealDishes());
    }
}
